/*---------------------------------------------------------------------------
	ZaRan	-	A Totallly Automatic CFD Software
	Copyright (C) ,Since 2020
-------------------------------------------------------------------------------
License
	This file is part of ZaRan.

!	@file		gird.h
!	@brief	the purpose of this file.
!	@author	Chen Jie.
\*---------------------------------------------------------------------------*/
#include"model/include/segment.h"
#include"Basic/include/CommonPara.h"
ModSeg::ModSeg(vector<Point>& vertex_)
{
	vertex_ = vertex_;
	vertexIndex_.resize(vertex_.size());
}

bool ModSeg::IsInModel(const Point& pt)
{
	return false;
}

void ModSeg::GenModelPoint(const double delta)
{
	double dis, dx, dy;
	modelPoint_.push_back(vertex_[0]);
	vertexIndex_[0] = 0;
	for (int iVertex = 0; iVertex < vertex_.size() - 1; ++iVertex)
	{
		dis = distance(vertex_[iVertex], vertex_[iVertex + 1]);
		dx = (vertex_[iVertex + 1].x() - vertex_[iVertex].x()) * delta / dis;
		dy = (vertex_[iVertex + 1].y() - vertex_[iVertex].y()) * delta / dis;
		while (distance(modelPoint_[modelPoint_.size() - 1], vertex_[iVertex + 1]) > delta)
			modelPoint_.push_back({ modelPoint_[modelPoint_.size() - 1].x() + dx,modelPoint_[modelPoint_.size() - 1].y() + dy });
		modelPoint_.push_back(vertex_[iVertex + 1]);
		modelPoint_[iVertex + 1] = modelPoint_.size() - 1;
	}
}
Point ModSeg::GetClosestPoint(const Point& pt)
{
	int iPoint, id = -1;
	double mindis = DBL_MAX;
	double dis;
	for (iPoint = 0; iPoint < modelPoint_.size(); ++iPoint)
	{
		dis = distance(modelPoint_[iPoint], pt);
		mindis = std::min(mindis, dis);
		if (abs(mindis - dis) < SMALL_NUMBER)
			id = iPoint;
	}
	return modelPoint_[id];
}
double ModSeg::NearestDistance(const Point& pt)
{
	int iVertex, jVertex;
	double minDistance = DBL_MAX;
	for (iVertex = 0, jVertex = vertex_.size() - 1; iVertex < vertex_.size(); jVertex = iVertex++)
	{
		Segment seg(vertex_[iVertex], vertex_[jVertex]);
		minDistance = std::min(minDistance, seg.Distance(pt));
	}
	return minDistance;
}



